package com.ls.blog.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class RabbitConfig {

    public static final  String LOG_EXCHANGE = "log_exchange";
    public static final  String COMMON_LOG_QUEUE = "common_log_queue";
    public static final  String EXCEPTION_LOG_QUEUE = "exception_log_queue";

    public static final  String DELAY_EXCHANGE = "delay_exchange";
    public static final  String DELAY_QUEUE = "delay_queue";
    public static final  String DEAD_EXCHANGE = "dead_exchange";
    public static final  String DEAD_QUEUE = "dead_queue";



    @Bean
    public Queue emailQueue(){
        return new Queue("emailQueue");
    }


    @Bean
    public Queue articleQueue(){
        return new Queue("articleQueue",true);
    }


    /**
     * 声明处理正常日志队列
     * @return
     */
    @Bean
    public Queue commonLogQueue(){
        return new Queue(COMMON_LOG_QUEUE,true);
    }


    /**
     * 声明处理异常日志队列
     * @return
     */
    @Bean
    public Queue exceptionLogQueue(){
        return new Queue(EXCEPTION_LOG_QUEUE,true);
    }

    /**
     * 声明日志交换机
     * @return
     */
    @Bean(LOG_EXCHANGE)
    public DirectExchange logExchange(){
        return new DirectExchange(LOG_EXCHANGE);
    }

    /**
     * 声明正常日志队列和交换机的绑定关系
     */
    @Bean
    public Binding queueCommonLogBind(){
        return BindingBuilder
                .bind(commonLogQueue())
                .to(logExchange())
                .with("common");
    }


    @Bean
    public Binding queueExceptionLogBind(){
        return BindingBuilder
                .bind(exceptionLogQueue())
                .to(logExchange())
                .with("exception");
    }

    // 声名死信交换机
    @Bean(DEAD_EXCHANGE)
    public DirectExchange deadExchange(){
        return new DirectExchange(DEAD_EXCHANGE);
    }


    @Bean(DEAD_QUEUE)
    public Queue deadQueue(){
        return new Queue(DEAD_QUEUE);
    }

    // 生命死信队列和死信交换机的绑定关系
    @Bean
    public Binding deadQueueBindDeadExchange(){
        return BindingBuilder
                .bind(deadQueue())
                .to(deadExchange())
                .with("YD");
    }


    // 声明一个交换机
    @Bean(DELAY_EXCHANGE)
    public DirectExchange delayExchange(){
        return new DirectExchange(DELAY_EXCHANGE);
    }

    // 声明延迟队列
    @Bean(DELAY_QUEUE)
    public Queue delayQueue(){
        Map<String, Object> args = new HashMap<>(3);
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        //声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", "YD");
        //声明队列的TTL
        args.put("x-message-ttl", 5000);
        return QueueBuilder.durable(DELAY_QUEUE).withArguments(args).build();
    }

    @Bean
    public Binding delayQueueBindDelayExchange(@Qualifier(DELAY_EXCHANGE) DirectExchange directExchange,@Qualifier(DELAY_QUEUE) Queue queue){
        return BindingBuilder
                .bind(queue)
                .to(directExchange)
                .with("YC");
    }









}
